﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace KELT.AutoColor
{
    public class ColorBoxManager
    {
        private StackPanel colorBoxListPanel;
        private AutoColorWindow mainContext;
        public ColorBoxItem selectItem;
        
        private static ObservableCollection<ColorBoxItem> m_colorBoxList = new ObservableCollection<ColorBoxItem>();       // 텍스트 객체 저장 list

        private Rectangle[] selectBox;
        private int SelectColorNumber = 0;
        public ObservableCollection<ColorBoxItem> colorBoxList
        {
            get { return m_colorBoxList; }
            set { m_colorBoxList = value; }
        }
        

        public ColorBoxManager(AutoColorWindow mainContext, StackPanel colorBoxList)
        {
            this.mainContext = mainContext;
            this.colorBoxListPanel = colorBoxList;
            selectBox = new Rectangle[5];
            
            initColorBox();
        }

        private void initColorBox()
        {
            byte[] colorBytes = new byte[3];
            Color randomColor = Color.FromRgb(colorBytes[0], colorBytes[1], colorBytes[2]);


            CreateColorBox("GreenBlueMadness", new Color[] { 
                Color.FromRgb(colorBytes[0] = 4, colorBytes[1] = 31, colorBytes[2] = 61), 
                Color.FromRgb(colorBytes[0] = 11, colorBytes[1] = 46, colorBytes[2] = 65), 
                Color.FromRgb(colorBytes[0] = 22, colorBytes[1] = 87, colorBytes[2] = 81), 
                Color.FromRgb(colorBytes[0] = 68, colorBytes[1] = 140, colorBytes[2] = 97), 
                Color.FromRgb(colorBytes[0] = 154, colorBytes[1] = 194, colorBytes[2] = 109) });

            CreateColorBox("Vanilla Coffee", new Color[] { 
                Color.FromRgb(colorBytes[0] = 168, colorBytes[1] = 186, colorBytes[2] = 169), 
                Color.FromRgb(colorBytes[0] = 255, colorBytes[1] = 245, colorBytes[2] = 207), 
                Color.FromRgb(colorBytes[0] = 219, colorBytes[1] = 205, colorBytes[2] = 173), 
                Color.FromRgb(colorBytes[0] = 179, colorBytes[1] = 156, colorBytes[2] = 125), 
                Color.FromRgb(colorBytes[0] = 128, colorBytes[1] = 104, colorBytes[2] = 84) });

            CreateColorBox("Flat Design Colors", new Color[] { 
                Color.FromRgb(colorBytes[0] = 51, colorBytes[1] = 77, colorBytes[2] = 92), 
                Color.FromRgb(colorBytes[0] = 69, colorBytes[1] = 178, colorBytes[2] = 157), 
                Color.FromRgb(colorBytes[0] = 239, colorBytes[1] = 201, colorBytes[2] = 76), 
                Color.FromRgb(colorBytes[0] = 226, colorBytes[1] = 122, colorBytes[2] = 63), 
                Color.FromRgb(colorBytes[0] = 223, colorBytes[1] = 90, colorBytes[2] = 73) });

            CreateColorBox("Sangmi's Colors1", new Color[] { 
                Color.FromRgb(colorBytes[0] = 244, colorBytes[1] = 178, colorBytes[2] = 68), 
                Color.FromRgb(colorBytes[0] = 240, colorBytes[1] = 51, colorBytes[2] = 64), 
                Color.FromRgb(colorBytes[0] = 242, colorBytes[1] = 237, colorBytes[2] = 208), 
                Color.FromRgb(colorBytes[0] = 22, colorBytes[1] = 203, colorBytes[2] = 184), 
                Color.FromRgb(colorBytes[0] = 0, colorBytes[1] = 142, colorBytes[2] = 154) });

            CreateColorBox("Sangmi's Colors2", new Color[] { 
                Color.FromRgb(colorBytes[0] = 0, colorBytes[1] = 128, colorBytes[2] = 194), 
                Color.FromRgb(colorBytes[0] = 0, colorBytes[1] = 170, colorBytes[2] = 143), 
                Color.FromRgb(colorBytes[0] = 131, colorBytes[1] = 214, colorBytes[2] = 197), 
                Color.FromRgb(colorBytes[0] = 255, colorBytes[1] = 238, colorBytes[2] = 213), 
                Color.FromRgb(colorBytes[0] = 255, colorBytes[1] = 222, colorBytes[2] = 173) });

            CreateColorBox("Sangmi's Colors3", new Color[] { 
                Color.FromRgb(colorBytes[0] = 141, colorBytes[1] = 255, colorBytes[2] = 169), 
                Color.FromRgb(colorBytes[0] = 232, colorBytes[1] = 213, colorBytes[2] = 127), 
                Color.FromRgb(colorBytes[0] = 255, colorBytes[1] = 128, colorBytes[2] = 127), 
                Color.FromRgb(colorBytes[0] = 205, colorBytes[1] = 116, colorBytes[2] = 232), 
                Color.FromRgb(colorBytes[0] = 142, colorBytes[1] = 167, colorBytes[2] = 255) });

            CreateColorBox("Sangmi's Colors4", new Color[] { 
                Color.FromRgb(colorBytes[0] = 31, colorBytes[1] = 34, colorBytes[2] = 38), 
                Color.FromRgb(colorBytes[0] = 244, colorBytes[1] = 28, colorBytes[2] = 84), 
                Color.FromRgb(colorBytes[0] = 255, colorBytes[1] = 249, colorBytes[2] = 232), 
                Color.FromRgb(colorBytes[0] = 25, colorBytes[1] = 194, colorBytes[2] = 222), 
                Color.FromRgb(colorBytes[0] = 254, colorBytes[1] = 253, colorBytes[2] = 249) });

            CreateColorBox("Theme1 Colors", new Color[] { 
                Color.FromRgb(colorBytes[0] = 224, colorBytes[1] = 73, colorBytes[2] = 93), 
                Color.FromRgb(colorBytes[0] = 224, colorBytes[1] = 138, colorBytes[2] = 187), 
                Color.FromRgb(colorBytes[0] = 224, colorBytes[1] = 194, colorBytes[2] = 187), 
                Color.FromRgb(colorBytes[0] = 245, colorBytes[1] = 225, colorBytes[2] = 201), 
                Color.FromRgb(colorBytes[0] = 217, colorBytes[1] = 217, colorBytes[2] = 217) });

            CreateColorBox("Theme2 Colors", new Color[] { 
                Color.FromRgb(colorBytes[0] = 230, colorBytes[1] = 226, colorBytes[2] = 175), 
                Color.FromRgb(colorBytes[0] = 167, colorBytes[1] = 163, colorBytes[2] = 126), 
                Color.FromRgb(colorBytes[0] = 239, colorBytes[1] = 236, colorBytes[2] = 202), 
                Color.FromRgb(colorBytes[0] = 4, colorBytes[1] = 99, colorBytes[2] = 128), 
                Color.FromRgb(colorBytes[0] = 0, colorBytes[1] = 47, colorBytes[2] = 47) });

            CreateColorBox("Theme3 Colors", new Color[] { 
                Color.FromRgb(colorBytes[0] = 125, colorBytes[1] = 138, colorBytes[2] = 46), 
                Color.FromRgb(colorBytes[0] = 201, colorBytes[1] = 215, colorBytes[2] = 135), 
                Color.FromRgb(colorBytes[0] = 255, colorBytes[1] = 255, colorBytes[2] = 255), 
                Color.FromRgb(colorBytes[0] = 255, colorBytes[1] = 192, colorBytes[2] = 169), 
                Color.FromRgb(colorBytes[0] = 255, colorBytes[1] = 133, colorBytes[2] = 152) });

            CreateColorBox("Theme4 Colors", new Color[] { 
                Color.FromRgb(colorBytes[0] = 244, colorBytes[1] = 178, colorBytes[2] = 68), 
                Color.FromRgb(colorBytes[0] = 240, colorBytes[1] = 51, colorBytes[2] = 64), 
                Color.FromRgb(colorBytes[0] = 242, colorBytes[1] = 237, colorBytes[2] = 208), 
                Color.FromRgb(colorBytes[0] = 22, colorBytes[1] = 203, colorBytes[2] = 184), 
                Color.FromRgb(colorBytes[0] = 0, colorBytes[1] = 142, colorBytes[2] = 154) });

            CreateColorBox("Fruity Colors", new Color[] { 
                Color.FromRgb(colorBytes[0] = 232, colorBytes[1] = 152, colorBytes[2] = 12), 
                Color.FromRgb(colorBytes[0] = 177, colorBytes[1] = 245, colorBytes[2] = 67), 
                Color.FromRgb(colorBytes[0] = 242, colorBytes[1] = 252, colorBytes[2] = 0), 
                Color.FromRgb(colorBytes[0] = 255, colorBytes[1] = 94, colorBytes[2] = 0), 
                Color.FromRgb(colorBytes[0] = 52, colorBytes[1] = 0, colorBytes[2] = 9) });

            mainContext.selectColor1.Fill = new SolidColorBrush(colorBoxList.ElementAt(0).colors[0]);
            mainContext.selectColor2.Fill = new SolidColorBrush(colorBoxList.ElementAt(0).colors[1]);
            mainContext.selectColor3.Fill = new SolidColorBrush(colorBoxList.ElementAt(0).colors[2]);
            mainContext.selectColor4.Fill = new SolidColorBrush(colorBoxList.ElementAt(0).colors[3]);
            mainContext.selectColor5.Fill = new SolidColorBrush(colorBoxList.ElementAt(0).colors[4]);

            selectItem = colorBoxList.ElementAt(0);

            selectBox[0] = mainContext.selectColor1;
            selectBox[1] = mainContext.selectColor2;
            selectBox[2] = mainContext.selectColor3;
            selectBox[3] = mainContext.selectColor4;
            selectBox[4] = mainContext.selectColor5;
        }

        private void CreateColorBox(string name, Color[] colors)
        {
            ColorBoxItem colorBoxItem = new ColorBoxItem(name, colors);
            colorBoxItem.CreateColorItem(this, colorBoxListPanel);
            colorBoxList.Add(colorBoxItem);
        }

        public void selectItemBoxEvent(object sender, MouseButtonEventArgs e)
        {

            for (int i = 0; i < colorBoxList.Count; i++ )
            {
                if(sender == colorBoxList.ElementAt(i).itemList == false)
                {
                    colorBoxList.ElementAt(i).isSelect = false;
                    DoubleAnimation opacityAnimation = new DoubleAnimation(0.5, TimeSpan.FromSeconds(0.5));
                    colorBoxList.ElementAt(i).itemList.BeginAnimation(StackPanel.OpacityProperty, opacityAnimation);

                }else
                {
                    colorBoxList.ElementAt(i).isSelect = true;
                    DoubleAnimation opacityAnimation = new DoubleAnimation(1, TimeSpan.FromSeconds(0.5));
                    colorBoxList.ElementAt(i).itemList.BeginAnimation(StackPanel.OpacityProperty, opacityAnimation);


                    mainContext.selectColor1.Fill = new SolidColorBrush(colorBoxList.ElementAt(i).colors[0]);
                    mainContext.selectColor2.Fill = new SolidColorBrush(colorBoxList.ElementAt(i).colors[1]);
                    mainContext.selectColor3.Fill = new SolidColorBrush(colorBoxList.ElementAt(i).colors[2]);
                    mainContext.selectColor4.Fill = new SolidColorBrush(colorBoxList.ElementAt(i).colors[3]);
                    mainContext.selectColor5.Fill = new SolidColorBrush(colorBoxList.ElementAt(i).colors[4]);


                    selectItem = colorBoxList.ElementAt(i);
                    setSlider(selectItem.colors[0].R, selectItem.colors[0].G, selectItem.colors[0].B);
                }
            }

            for (int i = 0; i < selectBox.Length; i++)
            {

                DoubleAnimation opacityAnimation = new DoubleAnimation(1, TimeSpan.FromSeconds(0.5));
                selectBox[i].BeginAnimation(StackPanel.OpacityProperty, opacityAnimation);
            }
        }
        public void setColor()
        {

            byte R = (byte)mainContext.RedSlider.Value;
            byte G = (byte)mainContext.GreenSlider.Value;
            byte B = (byte)mainContext.BlueSlider.Value;
            selectItem.colors[SelectColorNumber].R = R;
            selectItem.colors[SelectColorNumber].G = G;
            selectItem.colors[SelectColorNumber].B = B;
            
            selectBox[SelectColorNumber].Fill = new SolidColorBrush(Color.FromRgb(R, G, B));
        }
        public void setSliderSelectColor(int i)
        {
            mainContext.RedSlider.Value = selectItem.colors[i].R;
            mainContext.GreenSlider.Value = selectItem.colors[i].G;
            mainContext.BlueSlider.Value = selectItem.colors[i].B;
            selectColorAnimation(i);
        }

        public void setSlider(Byte R, Byte G, Byte B)
        {
            mainContext.RedSlider.Value = R;
            mainContext.GreenSlider.Value = G;
            mainContext.BlueSlider.Value = B;
        }

        public void selectColorAnimation(int num)
        {
            for(int i=0; i<selectBox.Length; i++)
            {
                if(i == num)
                {
                    DoubleAnimation opacityAnimation = new DoubleAnimation(1, TimeSpan.FromSeconds(0.5));
                    selectBox[i].BeginAnimation(StackPanel.OpacityProperty, opacityAnimation);

                }else
                {
                    DoubleAnimation opacityAnimation = new DoubleAnimation(0.8, TimeSpan.FromSeconds(0.5));
                    selectBox[i].BeginAnimation(StackPanel.OpacityProperty, opacityAnimation);

                }
            }
            SelectColorNumber = num;

        }
    }
}
